<?php

namespace App\Listeners;

use App\Events\ProductActionEvent;
use App\Helpers\Logger\SLogger;
use App\Models\Orm\Partner_apply_log;
use App\Models\Orm\Partner_handle_log;
use App\Models\Orm\Platform;
use App\Models\Orm\Product_audit;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use DB;

class ProductListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  ProductActionEvent  $event
     * @return void
     */
    public function handle(ProductActionEvent $event)
    {
        //
//        dd($event ->input);
        $data['create_time'] = $event ->create_time;
        $data['create_ip'] = $event ->create_ip;
        $data['type'] = $event ->type;
        $data['handler'] = $event ->handler;
        $data['handler_id'] = $event ->handler_id;
        $data['handle_content'] = $event -> handle_content;
        $admin_users_id = $event ->admin_users_id;
        $product_audit_id = $event ->id;
        $pl = Platform::select("id","pl_name")->where('admin_users_id',$admin_users_id)->first();
        if(!isset($pl)){
            return false;
        }
        $data['platform_id']   = $pl->id;
        $data['platform_name'] = $pl->pl_name;

        if($data['type'] ==1){

            $applyData['create_time'] = $event ->create_time;
            $applyData['admin_users_id'] = $event ->handler_id;
            $applyData['admin_users'] = $event ->handler;
            $applyData['product_audit_id'] = $event ->id;
            $applyData['status'] = 1;

            $re = Partner_apply_log::where('product_audit_id',$product_audit_id)->first();
            if(isset($re) && $re -> status == 1){


                    Partner_apply_log::where('product_audit_id',$product_audit_id)->update($applyData);

            }else{
                Partner_apply_log::create($applyData);

            }
//
        }

        DB::beginTransaction();
        try
        {


            $result =  Partner_handle_log::create($data);
            if (!$result) {
                DB::rollback();
                SLogger::getStream()->error('合作用户操作统计失败');

            }
            else
            {
                DB::commit();
            }
        }
        catch (\Exception $e)
        {
            DB::rollBack();

            SLogger::getStream()->error('合作用户操作统计失败');
        }
        return false;

    }
}
